package com.cssl.common.vo.cart;

import lombok.ToString;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/*
    购物车VO
    需要计算的属性需要重写get,保证每次获取属性都会重新计算
 */
@ToString
public class CartVO {
    private List<CartItemVO> items;  //购物项集合
    private Integer countNum;  //商品件数(汇总购物车内总件数)
    private Integer countType;  //商品数量(汇总购物车内商品总个数)
    private BigDecimal totalAmount; //商品总价
    private BigDecimal reduce = new BigDecimal("0.00"); //减免价格

    public List<CartItemVO> getItems() {
        return items;
    }

    public void setItems(List<CartItemVO> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;
        if(items!=null && items.size()>0){
            for(CartItemVO item : items){
                count += item.getCount();
            }
        }
        return count;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public Integer getCountType() {
        return CollectionUtils.isEmpty(items)?0:items.size();
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        //计算选中的购物项总价
        if(!CollectionUtils.isEmpty(items)){
            for(CartItemVO item : items){
                if(item.getCheck()){
                    amount = amount.add(item.getTotalPrice());
                }
            }
        }
        //计算优惠后的价格
        return amount.subtract(getReduce());
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
